package com.linkstec.musiccloud.controller;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.linkstec.musiccloud.pojo.Comment;
import com.linkstec.musiccloud.pojo.ListSongUser;
import com.linkstec.musiccloud.pojo.ListenTime;
import com.linkstec.musiccloud.pojo.Song;
import com.linkstec.musiccloud.pojo.User;
import com.linkstec.musiccloud.service.CommentService;
import com.linkstec.musiccloud.service.SongService;
import com.linkstec.musiccloud.service.SonglistService;
import com.linkstec.musiccloud.vo.CommentVo;
import com.linkstec.musiccloud.vo.SongListVos;
import com.linkstec.musiccloud.vo.SongVos;
import com.sun.org.glassfish.external.statistics.CountStatistic;

@Controller
@RequestMapping("song")
public class SongController {
	
	@Autowired
	private SongService songService;   
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private SonglistService songlistService;
	

	/**     
	 * @Description:TODO 通过类型找歌曲
	 * @author: shuaibizhe
	 * @date:   2018年11月23日 下午3:16:23    
	 * @param type
	 * @param mv
	 * @return      
	 */  
	@RequestMapping(value="selectSongByType")
	public ModelAndView selectSongByType(int type,ModelAndView mv){
		List<Song> songList = null;
		try {
			songList = songService.selectSongByType(type);
		} catch (Exception e) {
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**     
	 * @Description:TODO 根据id查找歌曲 src
	 * @author: shuaibizhe
	 * @date:   2018年11月23日 下午4:25:39    
	 * @param songId
	 * @param mv
	 * @return      
	 */  
	@RequestMapping(value="selectSongById")
	public String selectSongById(int songId,RedirectAttributes model,HttpServletRequest request){
		ListenTime ltime = new ListenTime();
		User user = (User) request.getSession().getAttribute("loginUser");
		if (user!=null) {
			ltime.setUserId(user.getUserId());
			ltime.setSongId(songId);
			songService.ltime(ltime);
		}
		Song song = null;
		try {
			song = songService.selectSongById(songId);
			model.addFlashAttribute("src", song.getSrc());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/song/Comment?songId="+songId+"&type="+0;
	}
	/**
	 * @Description:TODO 通过名字模糊查询搜索歌曲
	 * @author: xulisa
	 * @date:   2018年11月23日 上午10:28:58    
	 * @param songName
	 * @param mv
	 * @return
	 */
	@RequestMapping(value="selectSong",method=RequestMethod.POST)
	public  ModelAndView selectSong(ModelAndView mv,Song songName){
		List<Song> songList = null;
		try {
			songList = songService.selectSong(songName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**
	 * @Description:TODO 跳转到评论页
	 * @author: xulisa
	 * @date:   2018年11月23日 下午3:49:32    
	 * @return
	 */
	@RequestMapping(value="toComment")
	public String toComment(){
		return "single";
	}
	
	/**
	 * @Description:TODO 查看歌曲所有评论  @ModelAttribute("src")String src
	 * @author: xulisa
	 * @date:   2018年11月23日 下午4:26:01    
	 * @param mv
	 * @param songId
	 * @return
	 */
	@RequestMapping(value="Comment")
	public ModelAndView allComment(ModelAndView mv,int songId,HttpServletRequest request,int type){
		List<CommentVo> comments = null;
		int counts = 0;
		Song song = new Song();
		List<SongListVos> list = null;//点收藏歌曲 返回歌单图片和名
		User user = (User) request.getSession().getAttribute("loginUser");//获取user 返回这个user的歌单
		try {
			counts = commentService.selectCounts(songId);
			comments = commentService.selectAllComment(songId);
			song = songService.selectSongById(songId);
			if (type==1) {
				list = songlistService.selectListByUser(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		mv.addObject("counts",counts);
		mv.addObject("comment",comments);
		mv.addObject("song",song);
		mv.addObject("list",list);
		mv.setViewName("single");
		return mv;
	}
	
	
	/**
	 * @Description:TODO 发表评论
	 * @author: xulisa
	 * @date:   2018年11月23日 下午3:51:06    
	 * @return
	 */
	@RequestMapping(value="addComment")
	public String comment(HttpServletRequest request,Comment comment){
		Comment comm = new Comment();
		User user = (User) request.getSession().getAttribute("loginUser");
		comment.setUserId(user.getUserId());
		try {
			comm = commentService.addComment(comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/song/Comment?songId="+comment.getSongId()+"&type="+0;
	}
	
	/**     
	 * @Description:TODO 点击歌单出现对应歌曲
	 * @author: shuaibizhe
	 * @date:   2018年11月23日 下午8:40:22    
	 * @return      
	 */  
	@RequestMapping(value="selectSongByList")
	public ModelAndView selectSongByList(ListSongUser listSongUser,ModelAndView mv){
		List<SongVos> songVos = null;
		try {
			songVos = songService.selectSongByList(listSongUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songVos);
		mv.addObject("imgpath",songVos.get(1).getImgpath());
		mv.addObject("listName",songVos.get(1).getListName());
		mv.addObject("songCounts",songVos.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**     
	 * @Description:TODO 热歌榜
	 * @author: shuaibizhe
	 * @date:   2018年11月24日 下午1:56:05    
	 * @return      
	 */  
	@RequestMapping(value="selectSongByCounts")
	public ModelAndView selectSongByCounts(ModelAndView mv){
		List<Song> songList = null;
		try {
			songList = songService.selectSongByCounts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**     
	 * @Description:TODO 新歌榜
	 * @author: shuaibizhe
	 * @date:   2018年11月24日 下午2:37:46    
	 * @param mv
	 * @return      
	 */  
	@RequestMapping(value="selectSongByCreate")
	public ModelAndView selectSongByCreate(ModelAndView mv){
		List<Song> songList = null;
		try {
			songList = songService.selectSongByCreate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**     
	 * @Description:TODO 每日推荐 随机十首歌
	 * @author: shuaibizhe
	 * @date:   2018年11月24日 下午4:34:02    
	 * @param mv
	 * @return      
	 */  
	@RequestMapping(value="selectSongByRandom")
	public ModelAndView selectSongByRandom(ModelAndView mv){
		List<Song> songList = null;
		try {
			songList = songService.selectAllSongCounts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	/**     
	 * @Description:TODO 热评榜
	 * @author: shuaibizhe
	 * @date:   2018年11月26日 上午9:34:14    
	 * @param mv
	 * @return      
	 */  
	@RequestMapping(value="selectSongByComment")
	public ModelAndView selectSongByComment(ModelAndView mv){
		List<Song> songList = null;
		try {
			songList = songService.selectSongByComment();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList",songList);
		mv.addObject("songCounts",songList.size());
		mv.setViewName("sports");
		return mv;
	}
	
	
}
